package com.example.pilipili.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pilipili.common.R;
import com.example.pilipili.pojo.entity.Like;
import com.example.pilipili.dao.LikeMapper;
import com.example.pilipili.service.LikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liyv
 * @since 2023-12-01
 */
@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements LikeService {
    @Autowired
    LikeMapper likeMapper ;

    @Override
    @Transactional
    public R<Like> click(Like like) {
        QueryWrapper<Like> qw = new QueryWrapper<>() ;
        qw.eq("uid",like.getUid()).eq("vid",like.getVid());
        if(!likeMapper.exists(qw)) { // 点赞
            like.setTime(LocalDate.now());
            likeMapper.insert(like) ;
            return R.ok(like) ;
        }
        // 取消点赞
        likeMapper.delete(qw) ;
        return R.ok();
    }

    @Override
    public boolean isLiked(int uid, int vid) {
        QueryWrapper<Like> qw = new QueryWrapper<>() ;
        qw.eq("uid",uid).eq("vid",vid);
        return likeMapper.exists(qw);
    }

    @Override
    public R UserLikesNum(int uid) {
        return R.ok(likeMapper.selectByVUid(uid));
    }
}
